<?php
namespace BP\Web;

require_once("BP/Difference/ContextHolderUtils.php");
require_once("BP/Tools/StringHelper.php");
require_once("javax/servlet/http/Cookie.php");
require_once("java/net/URLDecoder.php");
require_once("java/net/URLEncoder.php");

class GuestUser {
	public static function SignInOfGener_String_String_String_b ($guestNo, $guestName, $lang, $isRememberMe) // [String guestNo, String guestName, String lang, boolean isRememberMe]
	{
			/* match: String_String_String_String_String_b */
		GuestUser->SignInOfGener_String_String_String_String_String_b($guestNo, $guestName, "deptNo", "deptName", $lang, $isRememberMe);
	}
	public static function SignInOfGener_String_String_String_String_String_b ($guestNo, $guestName, $deptNo, $deptName, $lang, $isRememberMe) // [String guestNo, String guestName, String deptNo, String deptName, String lang, boolean isRememberMe]
	{
		if (($ContextHolderUtils->getInstance() == NULL))
		{
			$SystemConfig->setIsBSsystem( FALSE );
		}
		else
		{
			$SystemConfig->setIsBSsystem( TRUE );
		}
		GuestUser::setNo($guestNo);
		GuestUser::setName($guestName);
		GuestUser::setDeptNo($deptNo);
		GuestUser::setDeptName($deptName);
		$em = new Emp();
		$em->setNo("Guest");
		if (($em->RetrieveFromDBSources() == 0))
		{
			$em->setName("客人");
			$em->Insert();
		}
		\WebUser::setNo($em->getNo());
		\WebUser::setName($em->getName());
		\WebUser::setFK_Dept($em->getFK_Dept());
		\WebUser::setFK_DeptName($em->getFK_DeptText());
		\WebUser::setSysLang($lang);
		if ($BP->Sys->SystemConfig->getIsBSsystem())
		{
			$expiry = (((60 * 60) * 24) * 2);
			$ContextHolderUtils->addCookie("GuestNo", $expiry, $guestNo);
			$ContextHolderUtils->addCookie("GuestName", $expiry, $URLEncoder->encode($guestName, "UTF-8"));
			$ContextHolderUtils->addCookie("DeptNo", $expiry, $deptNo);
			$ContextHolderUtils->addCookie("DeptName", $expiry, $URLEncoder->encode($deptName, "UTF-8"));
			$ContextHolderUtils->addCookie("No", $expiry, "Guest");
			$ContextHolderUtils->addCookie("Name", $expiry, $URLEncoder->encode($em->getName(), "UTF-8"));
			if ($isRememberMe)
			{
				$ContextHolderUtils->addCookie("IsRememberMe", $expiry, "1");
			}
			else
			{
				$ContextHolderUtils->addCookie("IsRememberMe", $expiry, "0");
			}
			$ContextHolderUtils->addCookie("FK_Dept", $expiry, $em->getFK_Dept());
			$ContextHolderUtils->addCookie("FK_DeptName", $expiry, $URLEncoder->encode($em->getFK_DeptText(), "UTF-8"));
			$ContextHolderUtils->addCookie("Token", $expiry, GuestUser::getNoOfSessionID());
			$ContextHolderUtils->addCookie("SID", $expiry, GuestUser::getNoOfSessionID());
			$ContextHolderUtils->addCookie("Lang", $expiry, $lang);
			$ContextHolderUtils->addCookie("Style", $expiry, "0");
			$ContextHolderUtils->addCookie("Auth", $expiry, "");
		}
	}
	public static function getNoOfSessionID () 
	{
		$s = GuestUser->GetSessionByKey_String_String("No", NULL);
		if (($s . NULL))
		{
			return $ContextHolderUtils->getSession()->getId();
		}
		return $s;
	}
	public static function GetSessionByKey_String_String ($key, $isNullAsVal) // [String key, String isNullAsVal]
	{
		if (((GuestUser::getIsBSMode() && (NULL != $ContextHolderUtils->getRequest())) && (NULL != $ContextHolderUtils->getSession())))
		{
			$value = $ContextHolderUtils->getSession()->getAttribute($key);
			$str = ( (($value == NULL)) ? "" : $String->valueOf($value) );
			if (is_null($str) || $str == '')
			{
				$str = $isNullAsVal;
			}
			return $str;
		}
		else
		{
			if ((((($Current->Session->get($key) == NULL) || $Current->Session->get($key)->equals(""))) . ($isNullAsVal . NULL)))
			{
				return $isNullAsVal;
			}
			else
			{
				$str = $Current->Session->get($key);
				return $str;
			}
		}
	}
	protected static function getIsBSMode () 
	{
		if (($ContextHolderUtils->getRequest() == NULL))
		{
			return  FALSE ;
		}
		else
		{
			return  TRUE ;
		}
	}
	public static function GetSessionByKey_String_Object ($key, $defaultObjVal) // [String key, Object defaultObjVal]
	{
		if (((GuestUser::getIsBSMode() && (NULL != $ContextHolderUtils->getRequest())) && (NULL != $ContextHolderUtils->getSession())))
		{
			$value = $ContextHolderUtils->getSession()->getAttribute($key);
			$str = ( (($value == NULL)) ? "" : $String->valueOf($value) );
			if (is_null($str) || $str == '')
			{
				$str = $defaultObjVal;
			}
			return $str;
		}
		else
		{
			if ((((($Current->Session->get($key) == NULL) || $Current->Session->get($key)->equals(""))) && ($defaultObjVal != NULL)))
			{
				return $defaultObjVal;
			}
			else
			{
				$str = $Current->Session->get($key);
				return $str;
			}
		}
	}
	public static function SetSessionByKey ($key, $val) // [String key, Object val]
	{
		if (((GuestUser::getIsBSMode() && (NULL != $ContextHolderUtils->getRequest())) && (NULL != $ContextHolderUtils->getSession())))
		{
			$ContextHolderUtils->getSession()->setAttribute($key, $val);
		}
		else
		{
			$Current->SetSession($key, $val);
		}
	}
	abstract static function Exit (); 
	public static function GetValFromCookie ($valKey, $isNullAsVal, $isChinese) // [String valKey, String isNullAsVal, boolean isChinese]
	{
		if (!GuestUser::getIsBSMode())
		{
			return $Current->GetSessionStr($valKey, $isNullAsVal);
		}
		try 
		{
			$value = $ContextHolderUtils->getSession()->getAttribute($valKey);
			$v = ( (($value == NULL)) ? "" : $String->valueOf($value) );
			if (!(is_null($v) || $v == ''))
			{
				if ($isChinese)
				{
					$v = $URLDecoder->decode($v, "UTF-8");
				}
				return $v;
			}
		}
		catch (\Exception $e)
		{ /* empty */ }
		try 
		{
			$val = NULL;
			$cookie = $ContextHolderUtils->getCookie($valKey);
			if (($cookie != NULL))
			{
				if ($isChinese)
				{
					$val = $URLDecoder->decode($cookie->getValue(), "UTF-8");
				}
				else
				{
					$val = $cookie->getValue();
				}
			}
			if ($StringHelper->isNullOrEmpty($val))
			{
				return $isNullAsVal;
			}
			return $val;
		}
		catch (\Exception $e2)
		{
			$e2->printStackTrace();
			return $isNullAsVal;
		}
	}
	public static function getNo () 
	{
		$no = NULL;
		if ((($no . NULL) . $no->equals("")))
		{
			if ((GuestUser::getIsBSMode() ==  FALSE ))
			{
				return "admin";
			}
			GuestUser::setNo(GuestUser->GetSessionByKey_String_String("No", "CCS"));
			GuestUser::setName(GuestUser->GetSessionByKey_String_String("Name", "CCS"));
			if ($DataType->IsNullOrEmpty(GuestUser::getNo()))
			{
				throw new \RuntimeException("@err-002 Guest 登录信息丢失�?");
			}
			return GuestUser::getNo();
		}
		return $no;
	}
	public static function setNo ($value) // [String value]
	{
		GuestUser::SetSessionByKey("GuestNo", $value);
	}
	public static function getName () 
	{
		$val = GuestUser::GetValFromCookie("GuestName", NULL,  TRUE );
		if (($val . NULL))
		{
			throw new \RuntimeException("@err-001 GuestName 登录信息丢失�?");
		}
		return $val;
	}
	public static function setName ($value) // [String value]
	{
		GuestUser::SetSessionByKey("GuestName", $value);
	}
	public static function getDeptNo () 
	{
		$val = GuestUser::GetValFromCookie("DeptNo", NULL,  TRUE );
		if (($val . NULL))
		{
			throw new \RuntimeException("@err-003 DeptNo 登录信息丢失�?");
		}
		return $val;
	}
	public static function setDeptNo ($value) // [String value]
	{
		GuestUser::SetSessionByKey("DeptNo", $value);
	}
	public static function getDeptName () 
	{
		$val = GuestUser::GetValFromCookie("DeptName", NULL,  TRUE );
		if (($val . NULL))
		{
			throw new \RuntimeException("@err-002 DeptName 登录信息丢失�?");
		}
		return $val;
	}
	public static function setDeptName ($value) // [String value]
	{
		GuestUser::SetSessionByKey("DeptName", $value);
	}
	public static function getStyle () 
	{
		return GuestUser->GetSessionByKey_String_String("Style", "0");
	}
	public static function setStyle ($value) // [String value]
	{
		GuestUser::SetSessionByKey("Style", $value);
	}
}
?>
